/**
 * Copyright © 2013 Creativor Studio.
 * About license information, please see LICENSE.txt.
 */
package org.creativor.rayson.client;

import java.net.InetSocketAddress;

import org.creativor.rayson.api.Protocol;
import org.creativor.rayson.api.exception.IllegalProtocolException;
import org.creativor.rayson.share.annotation.NotNull;
import org.creativor.rayson.util.LangUtil;
import org.creativor.rayson.util.RpcUtil;

/**
 * Java client.
 * 
 * @author creativor
 */
public final class Rayson {
	/**
	 * Create an proxy of protocol.
	 * 
	 * @param serverAddr
	 * @param protocol
	 *            Protocol interface class. Must be an java Interface.
	 * @return An proxy that used to call remote server RPC invocation, which
	 *         defined in protocol.
	 * @throws IllegalProtocolException
	 *             If the protocol format is wrong.
	 * @throws IllegalArgumentException
	 *             If arguments format is wrong.
	 */
	public static <T extends Protocol> T createProxy(
			@NotNull final InetSocketAddress serverAddr,
			@NotNull final Class<T> protocol) throws IllegalProtocolException,
			IllegalArgumentException {
		LangUtil.testNullArgument(serverAddr, "server address");
		LangUtil.testNullArgument(protocol, "RPC protocol");
		if (!protocol.isInterface())
			throw new IllegalArgumentException(
					"RPC protocol must be an interface");
		RpcUtil.verifyProtocol(protocol);
		return Client.getClient().createProxy(serverAddr, protocol);
	}
}
